
<style lang="less">
    @import "index.less";
</style>
<template>
    <div>
        <Row>
            <Col span="24">
                <Card class="margin-bottom-10">
                    <Form inline>
                        <FormItem class="margin-bottom-0">
                            <Select v-model="searchConf.type" clearable placeholder="请选择类别" style="width:120px">
                                <Option :value="1">操作URL</Option>
                                <Option :value="2">用户昵称</Option>
                                <Option :value="3">用户ID</Option>
                            </Select>
                        </FormItem>
                        <FormItem class="margin-bottom-0">
                            <Input v-model="searchConf.keywords" placeholder=""></Input>
                        </FormItem>
                        <FormItem class="margin-bottom-0">
                            <Button type="primary" @click="search">{{ $t('find_button') }}/{{ $t('refresh_button') }}</Button>
                        </FormItem>
                    </Form>
                </Card>
            </Col>
        </Row>
        <!-- 按钮列表 -->
        <Row>
            <Col span="24">
                <Card>
                    <div class="margin-bottom-15">
                        <Button type="primary" v-has="'User/add'" @click="alertAdd" icon="md-add">{{ $t('add_button') }}</Button>
                    </div>
                    <div>
                        <Table :loading="listLoading" :columns="columnsList" :data="tableData" border disabled-hover></Table>
                    </div>
                    <div class="margin-top-15" style="text-align: center">
                        <Page :total="tableShow.listCount" :current="tableShow.currentPage"
                              :page-size="tableShow.pageSize" @on-change="changePage"
                              @on-page-size-change="changeSize" show-elevator show-sizer show-total></Page>
                    </div>
                </Card>
            </Col>
        </Row>
        <Modal v-model="modalSetting.show" width="668" :styles="{top: '30px'}" @on-visible-change="doCancel">
            <p slot="header" style="color:#2d8cf0">
                <Icon type="md-alert"></Icon>
                <span>{{formItem.id ? '编辑' : '新增'}}用户</span>
            </p>
            <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="80">
                <FormItem label="字段名称" prop="table">
                    <Input v-model="formItem.table" placeholder="请输入字段名称"></Input>
                </FormItem>
                <FormItem label="字段标题" prop="title">
                    <Input v-model="formItem.title" placeholder="请输入字段标题"></Input>
                </FormItem>
                <FormItem label="默认值" prop="value">
                    <Input v-model="formItem.value" type="password" placeholder="默认值"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel" class="margin-right-10">取消</Button>
                <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import './../public/header.vue'
    import expandRow from './table_expand.vue'
    import {formatDate} from '@/libs/tools'
    import IconChoose from '_c/icon-choose'
    import {getFieldList, addField, editField, delField} from '@/api/mysql'
    const editButton = (vm, h, currentRow, index) => {
        if (vm.buttonShow.edit) {
            return h('Button', {
                props: {
                    type: 'primary'
                },
                style: {
                    margin: '0 5px'
                },
                on: {
                    'click': () => {
                        vm.formItem.id = currentRow.id
                        vm.formItem.username = currentRow.username
                        vm.formItem.nickname = currentRow.nickname
                        vm.formItem.password = 'ApiAdmin'
                        getGroups().then(response => {
                            vm.groupList = response.data.data.list
                        })
                        vm.formItem.group_id = currentRow.group_id
                        vm.modalSetting.show = true
                        vm.modalSetting.index = index
                    }
                }
            }, vm.$t('edit_button'))
        }
    }
    const deleteButton = (vm, h, currentRow, index) => {
        if (vm.buttonShow.del) {
            return h('Poptip', {
                props: {
                    confirm: true,
                    title: '您确定要删除这条数据吗? ',
                    transfer: true
                },
                on: {
                    'on-ok': () => {
                        del(currentRow.id).then(response => {
                            vm.tableData.splice(index, 1)
                            vm.$Message.success(response.data.msg)
                        })
                        currentRow.loading = false
                    }
                }
            }, [
                h('Button', {
                    style: {
                        margin: '0 5px'
                    },
                    props: {
                        type: 'error',
                        placement: 'top',
                        loading: currentRow.isDeleting
                    }
                }, vm.$t('delete_button'))
            ])
        }
    }

    export default {
        name: 'index',
        components: {expandRow},
        data() {
            return {
                columnsList: [
                    // {
                    //     title: '序号',
                    //     type: 'expand',
                    //     width: 50,
                    //     render: (h, params) => {
                    //         return h(expandRow, {
                    //             props: {
                    //                 row: params.row
                    //             }
                    //         })
                    //     }
                    // },
                    {
                        title: '序号',
                        type: 'index',
                        width: 65,
                        align: 'center'
                    },
                    {
                        title: '表名',
                        align: 'center',
                        key: 'table'
                    },
                    {
                        title: '字段名称',
                        align: 'center',
                        key: 'name'
                    },
                    {
                        title: '是否显示',
                        align: 'center',
                        key: 'show'
                    },
                    {
                        title: '字段标题',
                        align: 'center',
                        key: 'title',
                        width: 120
                    },
                    {
                        title: '字段类型',
                        align: 'center',
                        key: 'type',
                        width: 100
                    },
                    {
                        title: '默认值',
                        align: 'center',
                        key: 'value',
                        width: 200
                    },
                    {
                        title: '额外选项',
                        align: 'center',
                        key:'options'
                    },
                    {
                        title: '提示说明',
                        align: 'center',
                        width: 120,
                        key:'tips'
                    },
                    {
                        title: '状态',
                        align: 'center',
                        key:'status'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 125,
                        render: (h, params) => {
                            return h('div', [
                                deleteButton(this, h, params.row, params.index)
                            ])
                        }
                    }
                ],
                tableData: [],
                tableShow: {
                    currentPage: 1,
                    pageSize: 10,
                    listCount: 0
                },
                searchConf: {
                    type: '',
                    keywords: '',
                    status: ''
                },
                modalSetting: {
                    show: false,
                    loading: false,
                    index: 0
                },
                ruleValidate: {
                    username: [
                        { required: true, message: '用户账号不能为空', trigger: 'blur' }
                    ],
                    nickname: [
                        { required: true, message: '用户昵称不能为空', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '用户密码不能为空', trigger: 'blur' }
                    ]
                },
                formItem: {
                    table: '',
                    title: '',
                    value: '',
                    id: 0
                },
                buttonShow: {
                    edit: true,
                    del: true,
                    changeStatus: true
                },
                listLoading: false
            }
        },
        created() {
            let vm = this
            vm.getFieldList()
            vm.hasRule('Log/del').then(res => {
                vm.buttonShow.del = res
            })
        },
        //新增
        alertAdd () {
            let vm = this
            getGroups().then(response => {
                vm.groupList = response.data.data.list
            })
            this.modalSetting.show = true
        },
        doCancel (data) {
            if (!data) {
                this.formItem.id = 0
                this.$refs['myForm'].resetFields()
                this.modalSetting.loading = false
                this.modalSetting.index = 0
            }
        },
        submit () {
            let vm = this
            this.$refs['myForm'].validate((valid) => {
                if (valid) {
                    vm.modalSetting.loading = true
                    if (vm.formItem.id === 0) {
                        add(vm.formItem).then(response => {
                            vm.$Message.success(response.data.msg)
                            vm.getList()
                            vm.cancel()
                        }).catch(() => {
                            vm.modalSetting.loading = false
                        })
                    } else {
                        edit(vm.formItem).then(response => {
                            vm.$Message.success(response.data.msg)
                            vm.getList()
                            vm.cancel()
                        }).catch(() => {
                            vm.modalSetting.loading = false
                        })
                    }
                }
            })
        },
        methods: {
            changePage(page) {
                this.tableShow.currentPage = page
                this.getFieldList()
            },
            changeSize(size) {
                this.tableShow.pageSize = size
                this.getFieldList()
            },
            search() {
                this.tableShow.currentPage = 1
                this.getFieldList()
            },
            getFieldList() {
                let vm = this
                let params = {
                    page: vm.tableShow.currentPage,
                    size: vm.tableShow.pageSize,
                    type: vm.searchConf.type,
                    keywords: vm.searchConf.keywords
                }
                vm.listLoading = true
                getFieldList(params).then(response => {
                    vm.tableData = response.data.data.list
                    vm.tableShow.listCount = response.data.data.count
                    vm.listLoading = false
                })
            }
        }
    }
</script>
